<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<jsp:include page="../frame/header.jsp"></jsp:include>

<title>优惠券管理</title>
</head>

<body>

	<div class="container-fluid">
		<div id="market" class="row-fluid">
			<div>
				<ul class="breadcrumb">
					<li><a href="#">系统管理</a> <span class="divider">/</span></li>
					<li><a href="#">优惠券管理</a></li>
				</ul>
			</div>

			<div class="box">
				<div class="box-header well" data-original-title>
					<h2>
						<i class="icon-th"></i> 优惠券信息列表
					</h2>

					<div class="box-icon">
						<a href="#" class="btn btn-setting btn-round"><i
							class="icon-cog"></i></a> <a href="#"
							class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						<a href="#" class="btn btn-close btn-round"><i
							class="icon-remove"></i></a>
					</div>
				</div>
				<div class="box-content">
					<div id="search-form">
						<form id="searchForm" method="post" class="form-search span12" action="<c:url value="/admin/coupon/search_coupon"/>">
							
							<div class="in-line">
								<label>优惠券名称：</label> 
								<input type="text" name="name" id="name" value="${searchForm.name }" 
								class=" search-query">
								<label>所属分类：</label> 
								<select id="liveSorts" class="" name="sortId">
									<option value="0">全部</option>
									<c:forEach var="sort" items="${sorts }">
										<option value="${sort.menuId }" <c:if test="${sort.menuId == searchForm.sortId }">selected</c:if> >${sort.menuName }</option>
									</c:forEach>
								</select>
								<label>所属商户：</label> 
								<select data-placeholder="全部" id="sellers"
									name="sellerId" data-rel="chosen" class="">
									<option value="0">全部</option>

								</select>
								<button type="button" id="searchBtn" class="btn">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
							</div>
							
							<input type="hidden" name="pageNum" id="pageNum" value="${searchForm.pageNum }">
							<input type="hidden" name="couponId" id="couponId" value="${searchForm.couponId }">
							
							<button type="button" id="newBtn" class="btn btn-danger pull-right">添加优惠券</button>
						</form>
						
					</div>
					<table id="userInfo" class="table table-striped table-bordered ">
						<thead>
							<tr>
								<th>序号</th>
								<th>优惠券名称</th>
								<th>所属分类</th>
								<th>所属商户</th>
								<th>种类</th>
								<th>发行量</th>
								<th>领取量</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach var="info" items="${pageModel.result }" varStatus="status">
								<tr>
									<td>${status.index+1 }</td>
									<td>${info.name }</td>
									<td>${info.sortName } </td>
									<td>${info.sellerName } </td>
									<td>
										<c:choose>
											<c:when test="${info.type == 0 }">
												日领取版
											</c:when>
											<c:when test="${info.type == 1 }">
												限量版
											</c:when>
										</c:choose>
									</td>
									<td>${info.totalCount }</td>
									<td>${info.count }</td>
									<td><a href="javascript:edit('${info.couponId }')"><i
											class="icon-edit"></i></a> &nbsp;&nbsp; <a
										href="javascript:del('${info.couponId }')"><i class="icon-minus"></i></a></td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
					
					<div id="mypager" class="mypager"></div>
				</div>
			</div>
		</div>
		<!--/fluid-row-->

	</div>
	<!--/.fluid-container-->

	<jsp:include page="../frame/scripts.jsp"></jsp:include>
	<script type="text/javascript">
	var pageNum = "${pageModel.pageNum}";
	var totalPage = "${pageModel.totalPage}";
	var numPerPage = "${pageModel.numPerPage}";
	
	$(document).ready(function(){
		
		$("#newBtn").click(function(){
			//跳转至新增页面
			window.location.href = '<c:url value="/admin/coupon/addCoupon" />';
		})
		
		$('#liveSorts').change(function(){
				var sortId = $(this).val();
				$.ajax({
					url:'<c:url value="/admin/coupon/sellers" />',
					dataType:'json',
					data:{sortId:sortId},
					success:function(data){
						$("#sellers").html('<option value="0">全部</option>');
						//搜索表单初始化版块下拉框
						createTree(data);
						//设置选中
						$("#sellers").val("${searchForm.sellerId}");
						
						
					}
				})
			}).trigger('change');
		
		$('#mypager').bootstrapPaginator({
			currentPage:pageNum,
			totalPages:totalPage,
			numberOfPages:numPerPage,
			onPageClicked:function(event, originalEvent, type,page){
				$("input[name='pageNum']").val(page);
				$("#searchForm").submit();
			}
		});
		
		$('#searchBtn').click(function(){
			$("#searchForm").submit();
		})
	})
	
	function edit(couponId){
		window.location.href = '<c:url value="/admin/coupon/editCoupon?couponId='+couponId+'"/>';
	}
	
	function del(couponId){
		if(confirm("确定要删除吗？")){
			$("input[name='couponId']").val(couponId);
			$("#searchForm").submit();
		}
	}
	
	function createTree(data){
		var str = '';
		$.each(data,function(i,item){
			str += '<option value='+item.sellerId+'>'+item.name+'</option>';
		})
		$("#sellers").append(str);
	}
	
	</script>
</body>
</html>
